/*
  Copyright 2009 Smart Enterprise Solution Corp.
  Email: contact@ses.vn - Website: http://www.ses.vn
  KimNgan Project.
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using SES.CMS.DO;
/// <summary>
/// Summary description for cmsMenuDAL
/// </summary>
namespace SES.CMS.DAL
{

    public class cmsMenuDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public cmsMenuDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(cmsMenuDO objcmsMenuDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@Title", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsMenuDO.Title;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Link", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsMenuDO.Link;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Type", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.Type;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@OrderID", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.OrderID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@RootLevel", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.RootLevel;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ParentID", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.ParentID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Module", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.Module;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Icon", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsMenuDO.Icon;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(cmsMenuDO objcmsMenuDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@MenuID", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.MenuID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Title", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsMenuDO.Title;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Link", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsMenuDO.Link;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Type", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.Type;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@OrderID", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.OrderID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@RootLevel", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.RootLevel;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ParentID", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.ParentID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Module", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.Module;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Icon", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsMenuDO.Icon;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }

        public int Delete(cmsMenuDO objcmsMenuDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@MenuID", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.MenuID;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public cmsMenuDO Select(cmsMenuDO objcmsMenuDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@MenuID", SqlDbType.Int);
            Sqlparam.Value = objcmsMenuDO.MenuID;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["MenuID"]))
                    objcmsMenuDO.MenuID = Convert.ToInt32(dr["MenuID"]);
                if (!Convert.IsDBNull(dr["Title"]))
                    objcmsMenuDO.Title = Convert.ToString(dr["Title"]);
                if (!Convert.IsDBNull(dr["Link"]))
                    objcmsMenuDO.Link = Convert.ToString(dr["Link"]);
                if (!Convert.IsDBNull(dr["Type"]))
                    objcmsMenuDO.Type = Convert.ToInt32(dr["Type"]);
                if (!Convert.IsDBNull(dr["OrderID"]))
                    objcmsMenuDO.OrderID = Convert.ToInt32(dr["OrderID"]);
                if (!Convert.IsDBNull(dr["RootLevel"]))
                    objcmsMenuDO.RootLevel = Convert.ToInt32(dr["RootLevel"]);
                if (!Convert.IsDBNull(dr["ParentID"]))
                    objcmsMenuDO.ParentID = Convert.ToInt32(dr["ParentID"]);
                if (!Convert.IsDBNull(dr["Module"]))
                    objcmsMenuDO.Module = Convert.ToInt32(dr["Module"]);
                if (!Convert.IsDBNull(dr["Icon"]))
                    objcmsMenuDO.Icon = Convert.ToString(dr["Icon"]);

            }
            return objcmsMenuDO;
        }
        public DataTable Selectmenu(int ParentID)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_Selectmenu";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@Type", SqlDbType.Int);
            Sqlparam.Value = ParentID;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;

        }
        public ArrayList SelectAll1()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrcmsMenuDO = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    cmsMenuDO objcmsMenuDO = new cmsMenuDO();
                    if (!Convert.IsDBNull(dr["MenuID"]))
                        objcmsMenuDO.MenuID = Convert.ToInt32(dr["MenuID"]);
                    if (!Convert.IsDBNull(dr["Title"]))
                        objcmsMenuDO.Title = Convert.ToString(dr["Title"]);
                    if (!Convert.IsDBNull(dr["Link"]))
                        objcmsMenuDO.Link = Convert.ToString(dr["Link"]);
                    if (!Convert.IsDBNull(dr["Type"]))
                        objcmsMenuDO.Type = Convert.ToInt32(dr["Type"]);
                    if (!Convert.IsDBNull(dr["OrderID"]))
                        objcmsMenuDO.OrderID = Convert.ToInt32(dr["OrderID"]);
                    if (!Convert.IsDBNull(dr["RootLevel"]))
                        objcmsMenuDO.RootLevel = Convert.ToInt32(dr["RootLevel"]);
                    if (!Convert.IsDBNull(dr["ParentID"]))
                        objcmsMenuDO.ParentID = Convert.ToInt32(dr["ParentID"]);
                    if (!Convert.IsDBNull(dr["Module"]))
                        objcmsMenuDO.Module = Convert.ToInt32(dr["Module"]);
                    if (!Convert.IsDBNull(dr["Icon"]))
                        objcmsMenuDO.Icon = Convert.ToString(dr["Icon"]);
                    arrcmsMenuDO.Add(objcmsMenuDO);
                }
            }
            return arrcmsMenuDO;
        }

        public DataTable SelectAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }

        public DataTable SelectMenu(int ParentID)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_GetMenu";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@ParentID", SqlDbType.Int);
            Sqlparam.Value = ParentID;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;

        }
        public DataTable SelectMenu(int type, int ParentID)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_GetByType";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@Type", SqlDbType.Int);
            Sqlparam.Value = type;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ParentID", SqlDbType.Int);
            Sqlparam.Value = ParentID;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;

        }
        #endregion


        public cmsMenuDO SelectByURL(string url)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsMenu_GetByURL";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@URL", SqlDbType.NVarChar);
            Sqlparam.Value = url;
            Sqlcomm.Parameters.Add(Sqlparam);

            cmsMenuDO objcmsMenuDO = new cmsMenuDO();

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["MenuID"]))
                    objcmsMenuDO.MenuID = Convert.ToInt32(dr["MenuID"]);
                if (!Convert.IsDBNull(dr["Title"]))
                    objcmsMenuDO.Title = Convert.ToString(dr["Title"]);
                if (!Convert.IsDBNull(dr["Link"]))
                    objcmsMenuDO.Link = Convert.ToString(dr["Link"]);
                if (!Convert.IsDBNull(dr["Type"]))
                    objcmsMenuDO.Type = Convert.ToInt32(dr["Type"]);
                if (!Convert.IsDBNull(dr["OrderID"]))
                    objcmsMenuDO.OrderID = Convert.ToInt32(dr["OrderID"]);
                if (!Convert.IsDBNull(dr["RootLevel"]))
                    objcmsMenuDO.RootLevel = Convert.ToInt32(dr["RootLevel"]);
                if (!Convert.IsDBNull(dr["ParentID"]))
                    objcmsMenuDO.ParentID = Convert.ToInt32(dr["ParentID"]);
                if (!Convert.IsDBNull(dr["Module"]))
                    objcmsMenuDO.Module = Convert.ToInt32(dr["Module"]);
                if (!Convert.IsDBNull(dr["Icon"]))
                    objcmsMenuDO.Icon = Convert.ToString(dr["Icon"]);

            }
            return objcmsMenuDO;
        }
    }

}
